package com.surveypark.struts2.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.surveypark.domain.Survey;
import com.surveypark.domain.User;
import com.surveypark.service.SurveyService;
import com.surveypark.util.ValidateUtil;

@Controller("surveyAction")
@Scope("prototype")
public class SurveyAction extends BaseAction<Survey> implements UserAware, ServletContextAware{

	private static final long serialVersionUID = -8988086258935044345L;
	
	@Resource(name="surveyService")
	private SurveyService surveyService;
	private User user;
	private List<Survey> mySurveys;
	private Integer sid;
	
	/*ͼƬ�ϴ����
	 * 
	 */
	private File logoPhoto; 
	private String logoPhotoFileName;
	private String logoPhotoContentType;

	//input页面
	private String inputPage;
	
	public String getInputPage() {
		return inputPage;
	}
	public void setInputPage(String inputPage) {
		this.inputPage = inputPage;
	}


	private ServletContext servletContext;

	private int surveyToo;//调查状态

	
	public int getSurveyToo() {
		return surveyToo;
	}
	public void setSurveyToo(int surveyToo) {
		this.surveyToo = surveyToo;
	}
	
	public File getLogoPhoto() {
		return logoPhoto;
	}
	public void setLogoPhoto(File logoPhoto) {
		this.logoPhoto = logoPhoto;
	}
	public String getLogoPhotoFileName() {
		return logoPhotoFileName;
	}
	public void setLogoPhotoFileName(String logoPhotoFileName) {
		this.logoPhotoFileName = logoPhotoFileName;
	}
	public String getLogoPhotoContentType() {
		return logoPhotoContentType;
	}
	public void setLogoPhotoContentType(String logoPhotoContentType) {
		this.logoPhotoContentType = logoPhotoContentType;
	}
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	@Override
	public void setUser(User user) {

		this.user = user;
	}
	public List<Survey> getMySurveys() {
		return mySurveys;
	}
	public void setMySurveys(List<Survey> mySurveys) {
		this.mySurveys = mySurveys;
	}
	
	/**
	 *	新建调查
	 */
	public String newSurvey(){
		
		model = surveyService.newSurvey(user);
		return "designSurveyPage";
	}
	
	/**
	 * 我的调查
	 */
	public String mySurveys(){
		this.mySurveys = surveyService.findMySurveys(user);
		return "mySurveyListPage";
	}

	/**
	 * 设计调查
	 */
	public String designSurvey(){
		this.model = surveyService.getSurveyWithChildren(sid);
		return "designSurveyPage";
	}
	
	/**
	 * 编辑调查
	 */
	public String editSurvey(){
		this.model = surveyService.getSurvey(sid);
		return "editSurveyPage";
	}
	
	/**
	 * 更新调查
	 */
	public String updateSurvey(){
		this.sid = model.getId();
		model.setUser(user);
		surveyService.updateSurvey(model);
		return "designSurveyAction";
	}
	
	/**
	 */
//	public void prepareDesignSurvey(){
//		
//		this.model = surveyService.getEntity(sid);
//	}
	
	public void prepareDoAddLogo(){
		this.inputPage = "/WEB-INF/jsp/survey/addLogo.jsp";
	}
	
	/**
	 * 删除调查
	 */
	public String deleteSurvey(){
	
		surveyService.deleteSurvey(sid);
		return "findMySurveysAction";
	}
	
	/**
	 * 清空调查
	 */
	public String clearAnswers(){
		
		surveyService.clearAnswers(sid);
		return SUCCESS;
	}
	
	/**
	 * 打开/关闭调查
	 */
	public String toggleStatus(){
		if(surveyService.toggleStatus(sid)){
			this.surveyToo = 1;//关闭
		}else
			this.surveyToo = 0;//开放          
		return SUCCESS;
	}
	
	/**
	 * 增加logo
	 */
	public String toAddLogoPage(){
		
		return "addLogoPage";
	}
	
	/**
	 * 处理增加logo
	 */
	public String doAddLogo(){
		
		if(ValidateUtil.isValid(logoPhotoFileName)){
			String dir = servletContext.getRealPath("/upload");
			long time = System.nanoTime();
			String ext = logoPhotoFileName.substring(logoPhotoFileName.indexOf("."));
			File file = new File(dir, time + ext);
			logoPhoto.renameTo(file);
			//更新图片路径
			surveyService.updateLogoPhotoPath(sid, "/upload/" + time + ext);
		}
		return "designSurveyAction";
	}
	
	/**
	 * 判断图片是否存在
	 */
	public boolean logoPhotoExists(){
		String path = model.getLogoPhotoPath();
		if(ValidateUtil.isValid(path)){
			String realPath = servletContext.getRealPath(path);
			return new File(realPath).exists();
		}
		return false;
	}
	
	/**
	 * 分析调查
	 */
	public String analyzeSurvey(){
		this.model = surveyService.getSurveyWithChildren(sid);
		return "analyzeSurveyListPage";
	}
	
	@Override
	public void setServletContext(ServletContext context) {
		this.servletContext = context;
	}
}
